# Copyright (C) The libssh2 project and its contributors.
# SPDX-License-Identifier: BSD-3-Clause

option(LIBSSH2_USE_PKGCONFIG "Enable pkg-config to detect @PROJECT_NAME@ dependencies. Default: @LIBSSH2_USE_PKGCONFIG@" "@LIBSSH2_USE_PKGCONFIG@")

include(CMakeFindDependencyMacro)
list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})

set(_libdirs "")
set(_libs "")

if("@CRYPTO_BACKEND@" STREQUAL "OpenSSL")
  find_dependency(OpenSSL)
elseif("@CRYPTO_BACKEND@" STREQUAL "wolfSSL")
  find_dependency(WolfSSL)
  if(WOLFSSL_FOUND)
    list(APPEND _libdirs ${WOLFSSL_LIBRARY_DIRS})
    list(APPEND _libs ${WOLFSSL_LIBRARIES})
  endif()
elseif("@CRYPTO_BACKEND@" STREQUAL "Libgcrypt")
  find_dependency(Libgcrypt)
  if(LIBGCRYPT_FOUND)
    list(APPEND _libdirs ${LIBGCRYPT_LIBRARY_DIRS})
    list(APPEND _libs ${LIBGCRYPT_LIBRARIES})
  endif()
elseif("@CRYPTO_BACKEND@" STREQUAL "mbedTLS")
  find_dependency(MbedTLS)
  if(MBEDTLS_FOUND)
    list(APPEND _libdirs ${MBEDTLS_LIBRARY_DIRS})
    list(APPEND _libs ${MBEDTLS_LIBRARIES})
  endif()
endif()

if(@ZLIB_FOUND@)
  find_dependency(ZLIB)
endif()

include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake")

# Alias for either shared or static library
if(NOT TARGET @PROJECT_NAME@::@LIB_NAME@)
  add_library(@PROJECT_NAME@::@LIB_NAME@ ALIAS @PROJECT_NAME@::@LIB_SELECTED@)
endif()

# Compatibility alias
if(NOT TARGET Libssh2::@LIB_NAME@)
  add_library(Libssh2::@LIB_NAME@ ALIAS @PROJECT_NAME@::@LIB_SELECTED@)
endif()

foreach(_target IN ITEMS @PROJECT_NAME@::@LIB_SHARED@ @PROJECT_NAME@::@LIB_STATIC@)
  if(TARGET "${_target}")
    if(_libdirs)
      if(CMAKE_VERSION VERSION_LESS 3.13)
        set_target_properties("${_target}" PROPERTIES INTERFACE_LINK_DIRECTORIES "${_libdirs}")
      else()
        target_link_directories("${_target}" INTERFACE ${_libdirs})
      endif()
    endif()
    if(_libs)
      target_link_libraries("${_target}" INTERFACE ${_libs})
    endif()
  endif()
endforeach()
